package com.tongji.showroom.view.twoD
{
	import com.tongji.showroom.utils.ConstVariableInventory;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;
	
	
	/**
	 * 
	 * @author Barbiel
	 * 2011-11-3
	 */
	public class DistortSprite extends Sprite
	{	
		public var handle0:Sprite;
		private var handle1:Sprite;
		private var handle2:Sprite;
		private var handle3:Sprite;
		private var handle4:Sprite;
		private var handle5:Sprite;
		private var handle6:Sprite;
		private var handle7:Sprite;
		private var currentHandle:Sprite;
		
		private var dX:Number;
		private var dY:Number;
		private var ctrlKey:Boolean;
		private var vertices:Vector.<Number> = new Vector.<Number>();
		private var uvtData:Vector.<Number> = new Vector.<Number>();
		private var indices:Vector.<int> = new Vector.<int>();
		public var bitmap:Bitmap;
		private var mousex:Number;
		private var mousey:Number;
		private var isShow:Boolean = false;
		
		private var tfc:TransformCursor = new TransformCursor();   
		public var sprite:Sprite;
		public var filter:DropShadowFilter;
		private var filterArray:Array;
		public function DistortSprite(bitmap:Bitmap):void
		{
			/*stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;*/
			
			handle0 = makeHandle(0, 0);
			handle1 = makeHandle(bitmap.width, 0);
			handle2 = makeHandle(bitmap.width, bitmap.height);
			handle3 = makeHandle(0, bitmap.height);
			
			handle4 = makeHandle(bitmap.width / 2, 0);
			handle5 = makeHandle(bitmap.width, bitmap.height / 2);
			handle6 = makeHandle(bitmap.width / 2, bitmap.height);
			handle7 = makeHandle(bitmap.width, bitmap.height / 2);
			
			addDotOverOutEvent();
			
			uvtData.push(0, 0);
			uvtData.push(1, 0);
			uvtData.push(1, 1);
			uvtData.push(0, 1);
			
			indices.push(0, 1, 2);
			indices.push(2, 3, 0);
			this.bitmap = bitmap;
			
			isShowDot(isShow);
			sprite = new Sprite();
			addChild(sprite);
			
			/*sprite.addChild(bitmap);*/
			this.addChild(tfc);  

			
			sprite.addEventListener(MouseEvent.MOUSE_DOWN, showDot);
			
			filter = new DropShadowFilter();
			filter.alpha = ConstVariableInventory.DEFAULT_ALPHA;
			filter.angle = ConstVariableInventory.DEFAULT_ANGLE;
			filter.blurX = ConstVariableInventory.DEFAULT_BLURX;
			filter.blurY = ConstVariableInventory.DEFAULT_BLURY;
			filter.color = ConstVariableInventory.DEFAULT_COLOR;
			filter.distance = ConstVariableInventory.DEFAULT_DISTANCE;
			filter.quality = ConstVariableInventory.DEFAULT_QUALITY;
			filter.strength = ConstVariableInventory.DEFAULT_STRENGTH;
			filterArray = new Array();
			filterArray.push(filter);
			
			draw();
		}
		

		
		private function showDot(event:MouseEvent):void
		{
			isShow = true;
			isShowDot(isShow);
			startDrag();
			if (currentHandle == handle0 || currentHandle == handle1 || currentHandle == handle2 || currentHandle ==handle3 || currentHandle ==handle4 || currentHandle ==handle5 || currentHandle ==handle6 || currentHandle ==handle7)
			{
				
			}
			else
			{
				
			}
			//startDrag();
			//stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, quitEditHandler);
			sprite.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		
		protected function mouseUpHandler(event:MouseEvent):void
		{
			stopDrag();
		}
		
		protected function quitEditHandler(event:KeyboardEvent):void
		{
			isShow = false;
			if (event.keyCode == Keyboard.ENTER)
			{ 								
				isShowDot(false);
			}
		}
//		private function startMove(event:MouseEvent):void
//		{
//			startDrag();
//		}
//		

		private function makeHandle(xpos:Number, ypos:Number):Sprite
		{
			var handle:Sprite = new Sprite();
			handle.graphics.beginFill(0);
			handle.graphics.drawCircle(0, 0, 10);
			handle.graphics.endFill();
			handle.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			//handle.addEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			//handle.addEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle.x = xpos;
			handle.y = ypos;
			addChild(handle);
			
			return handle;
		}
		
		private function addDotOverOutEvent():void
		{
			handle0.addEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			handle1.addEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			handle2.addEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			handle3.addEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			handle4.addEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			handle5.addEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			handle6.addEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			handle7.addEventListener(MouseEvent.ROLL_OVER, dotRollOver);		
			handle0.addEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle1.addEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle2.addEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle3.addEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle4.addEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle5.addEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle6.addEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle7.addEventListener(MouseEvent.ROLL_OUT, dotRollOut);
		}
		
		private function removeDotOutEvent():void
		{
			handle0.removeEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			handle1.removeEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			handle2.removeEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			handle3.removeEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			handle4.removeEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			handle5.removeEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			handle6.removeEventListener(MouseEvent.ROLL_OVER, dotRollOver);
			handle7.removeEventListener(MouseEvent.ROLL_OVER, dotRollOver);		
			handle0.removeEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle1.removeEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle2.removeEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle3.removeEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle4.removeEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle5.removeEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle6.removeEventListener(MouseEvent.ROLL_OUT, dotRollOut);
			handle7.removeEventListener(MouseEvent.ROLL_OUT, dotRollOut);	
		}
		
		private function dotRollOut(event:MouseEvent):void
		{
			tfc.setCursor = TransformCursor.NORMAL;
		}
		
		private function dotRollOver(event:MouseEvent):void
		{
			
			currentHandle = event.target as Sprite;
			rTfc();
			tfc.setCursor = TransformCursor.FANXIE;
			switch(currentHandle)
			{
				case handle0:    //左上角
					tfc.setCursor = TransformCursor.FANXIE;
					break;
				case handle4:    //上侧中间的点
					tfc.setCursor = TransformCursor.TOPBOTTOM;
					break;
				case handle1:    //右上角
					tfc.setCursor = TransformCursor.ZHENGXIE;
					break;
				case handle7:    //左侧中间的点
					tfc.setCursor = TransformCursor.LEFTRIGHT;
					break;
				case handle5:    //右侧中间的点
					tfc.setCursor = TransformCursor.LEFTRIGHT;
					break;
				case handle3:    //左下角
					tfc.setCursor = TransformCursor.ZHENGXIE;
					break;
				case handle6:    //下侧中间的点
					tfc.setCursor = TransformCursor.TOPBOTTOM;
					break;
				case handle2:    //右下角
					tfc.setCursor = TransformCursor.FANXIE;
			}
		}
		
		private function onMouseDown(event:MouseEvent):void
		{
			/*stopDrag();*/
			currentHandle = event.target as Sprite;
		
			removeDotOutEvent();
			event.target.startDrag();
			mousex = mouseX;
			mousey = mouseY;
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		private function onMouseMove(event:MouseEvent):void
		{
			if (currentHandle ==handle0 || currentHandle ==handle1 || currentHandle ==handle2 || currentHandle ==handle3 || currentHandle ==handle4 || currentHandle ==handle5 || currentHandle ==handle6 || currentHandle ==handle7)
			{
				rTfc();
				ctrlKey = event.ctrlKey;
				draw();	
			}						
		}
		 
		private function onMouseUp(event:MouseEvent):void
		{
			event.target.stopDrag();	
			tfc.setCursor = TransformCursor.NORMAL;
			var rightNotOut:Boolean = (event.stageX + (event.target.width - event.localX) * ConstVariableInventory.X_SCALE - 47) < ConstVariableInventory.RECT_WIDTH;
			var leftNotOut:Boolean = (event.stageX - event.localX * ConstVariableInventory.X_SCALE - 47) > 0;
			var upNotOut:Boolean = (event.stageY - event.localY * ConstVariableInventory.Y_SCALE - 220) > 0;
			var downNotOut:Boolean = (event.stageY + (event.target.height - event.localY) * 450 / 768 - 220) < ConstVariableInventory.RECT_HEIGHT;

			addDotOverOutEvent();
			if (rightNotOut && leftNotOut && upNotOut && downNotOut)
			{
				stopDrag();		
				currentHandle = new Sprite;	
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
				stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
				tfc.setCursor = TransformCursor.NORMAL;
			}
			else
			{
				trace("出界");
			}
		}
		
		private function draw():void
		{
			if (ctrlKey)
			{
				if(currentHandle == handle4)
				{
					dX = handle4.x - (handle0.x + handle1.x) / 2;
					dY = handle4.y - (handle0.y + handle1.y) / 2;
					
					handle0.x += dX;
					handle0.y += dY;
					handle1.x += dX;
					handle1.y += dY;
				}
				else if(currentHandle == handle5)
				{
					dX = handle5.x - (handle2.x + handle1.x) / 2;
					dY = handle5.y - (handle2.y + handle1.y) / 2;
					
					handle2.x += dX;
					handle2.y += dY;
					handle1.x += dX;
					handle1.y += dY;
				}
				else if(currentHandle == handle6)
				{
					dX = handle6.x - (handle2.x + handle3.x) / 2;
					dY = handle6.y - (handle2.y + handle3.y) / 2;
					
					handle2.x += dX;
					handle2.y += dY;
					handle3.x += dX;
					handle3.y += dY;
				}
				else if(currentHandle == handle7)
				{
					dX = handle7.x - (handle0.x + handle3.x) / 2;
					dY = handle7.y - (handle0.y + handle3.y) / 2;
					
					handle0.x += dX;
					handle0.y += dY;
					handle3.x += dX;
					handle3.y += dY;
				}
			}
			else
			{
				if (currentHandle == handle0)
				{
					if (handle4.y == handle1.y && handle1.y != handle2.y)
					{						
						handle1.x = (handle1.x - handle2.x) / (handle1.y - handle2.y) * (handle0.y - handle1.y) + handle1.x;
						handle1.y = handle0.y;
					}
					else if (handle1.y == handle2.y && handle4.y != handle1.y)
					{
						if (handle1.x != handle4.x)
						{
							handle1.x = (handle1.x - handle4.x) / (handle1.y - handle4.y) * (handle1.y - handle0.y) + handle0.x;
						}
						else 
						{
							handle1.x = handle2.x;
						}
						handle1.y = handle1.y;
					}
					else if (handle1.y != handle4.y && handle1.y != handle2.y)
					{
						var k1:Number = (handle1.x - handle4.x) / (handle1.y - handle4.y);
						var k2:Number = (handle1.x - handle2.x) / (handle1.y - handle2.y);
						var temp:Number = handle1.y;
						handle1.y = (handle1.x -handle0.x + handle0.y * k1 - handle1.y * k2) / (k1 - k2);
						handle1.x = (handle1.x - handle4.x) / (temp - handle4.y) * (handle1.y - handle0.y) + handle0.x;
					}
					else
					{
						handle1.x = handle1.x;
						handle1.y = handle0.y + handle2.y - handle3.y;
					}
					//====================================================================================					
					if (handle7.x == handle3.x && handle7.x != handle2.x)
					{						
						handle3.x = (handle3.x - handle2.x) / (handle3.y - handle2.y) * (handle0.y - handle3.y) + handle3.x;
						handle3.x = handle0.x;
					}
					else if (handle3.x == handle2.x && handle7.x != handle2.x)
					{
						handle3.x = (handle3.x - handle7.x) / (handle3.y - handle7.y) * (handle3.y - handle0.y) + handle0.x;
						handle3.x = handle3.x;
					}
					else if (handle7.y != handle3.y && handle3.y != handle2.y)
					{
						var k1:Number = (handle3.x - handle7.x) / (handle3.y - handle7.y);
						var k2:Number = (handle3.x - handle2.x) / (handle3.y - handle2.y);
						var temp:Number = handle3.y;
						handle3.y = (handle3.x -handle0.x + handle0.y * k1 - handle3.y * k2) / (k1 - k2);
						handle3.x = (handle3.x - handle7.x) / (temp - handle7.y) * (handle3.y - handle0.y) + handle0.x;
						handle3.y = temp;
					}
					else
					{
						handle3.x = handle0.x + handle2.x - handle1.x;
						handle3.y = handle0.y + handle2.y - handle1.y;
					}
					//===========================================================================================
				}
				
				//---------------------------------------------
				if (currentHandle == handle1)
				{
					trace(handle0.x, handle0.y, handle1.x, handle1.y, handle2.x, handle2.y, handle3.x, handle3.y);
					if (handle5.x == handle2.x && handle2.x != handle3.x)
					{	
						trace("a");
						handle2.y = (handle2.y - handle3.y) / (handle2.x - handle3.x) * (handle1.x - handle2.x) + handle2.y;
						handle2.x = handle1.x;
					}
					else if (handle2.x == handle3.x && handle5.x != handle2.x)
					{
						trace("q");
						
						handle2.y = (handle2.y - handle5.y) / (handle2.x - handle5.x) * (handle2.x - handle1.x) + handle1.y;
						handle2.x = handle2.x;
					}
					else if (handle2.x != handle3.x && handle5.x != handle2.x)
					{
						trace("w");
						var k1:Number = (handle2.y - handle5.y) / (handle2.x - handle5.x);
						var k2:Number = (handle2.y - handle3.y) / (handle2.x - handle3.x);
						var temp:Number = handle2.x;
						handle2.x = (handle2.y - handle2.y + handle1.x * k1 - handle2.x * k2) / (k1 - k2);
						handle2.y = (handle2.y - handle5.y) / (temp - handle5.x) * (handle2.x - handle1.x) + handle1.y;
					}
					else
					{
						handle2.x = handle1.x + handle3.x - handle0.x;
						handle2.y = handle1.y + handle3.y - handle0.y;
					}
					
					if (handle4.y == handle0.y && handle0.y != handle3.y)
					{	
						trace("r");
						handle0.x = (handle0.x - handle3.x) / (handle0.y - handle3.y) * (handle1.y - handle0.y) + handle0.x;
						handle0.y = handle1.y;
					}
					else if (handle0.y == handle3.y && handle0.y != handle4.y)
					{
						trace("t");
						handle0.x = (handle0.x - handle4.x) / (handle0.y - handle4.y) * (handle0.y - handle1.y) + handle1.x;
						handle0.y = handle0.y;
					}
					else if (handle0.y != handle3.y && handle0.y != handle4.y)
					{
						trace("y");
						var k1:Number = (handle0.x - handle4.x) / (handle0.y - handle4.y);
						var k2:Number = (handle0.x - handle3.x) / (handle0.y - handle3.y);
						var temp:Number = handle0.y;
						handle0.y = (handle0.x -handle1.x + handle1.y * k1 - handle0.y * k2) / (k1 - k2);
						handle0.x = (handle0.x - handle4.x) / (temp - handle4.y) * (handle0.y - handle1.y) + handle1.x;
					}
					else
					{
						handle0.x = handle3.x + handle1.x - handle2.x;
						handle0.y = handle1.y ;
					}
				}
				//-------------------------------------
				if (currentHandle == handle2)
				{
					if (handle6.y == handle3.y && handle3.y != handle0.y)
					{		
						handle3.x = (handle3.x - handle0.x) / (handle3.y - handle0.y) * (handle2.y - handle3.y) + handle3.x;
						handle3.y = handle2.y;
						
					}
					else if (handle3.y == handle0.y && handle6.y != handle3.y)
					{
						trace("B");
						if (handle3.x != handle6.x)
						{
							handle3.x = (handle3.x - handle6.x) / (handle3.y - handle6.y) * (handle3.y - handle2.y) + handle2.x;
						}
						else
						{
							handle3.x =handle0.x;
						}
						handle3.y = handle3.y;
					}
					else if (handle3.y != handle6.y && handle3.y != handle0.y)
					{
						trace("a");		
						var k1:Number = (handle3.x - handle6.x) / (handle3.y - handle6.y);
						var k2:Number = (handle3.x - handle0.x) / (handle3.y - handle0.y);
						var temp:Number = handle3.y;
						handle3.y = (handle3.x -handle2.x + handle2.y * k1 - handle3.y * k2) / (k1 - k2);
						handle3.x = (handle3.x - handle6.x) / (temp - handle6.y) * (handle3.y - handle2.y) + handle2.x;
					}
					else 
					{
						trace(handle3.x);
						handle3.x = handle3.x;
						handle3.y = handle0.y + handle2.y - handle1.y;
					}
					
					if (handle5.y == handle1.y && handle1.y != handle0.y)
					{	
						trace("b");
						handle1.x = (handle1.x - handle0.x) / (handle1.y - handle0.y) * (handle2.y - handle1.y) + handle1.x;
						handle1.y = handle2.y;
					}
					else if (handle1.y == handle0.y && handle1.y != handle5.y)
					{
						trace("n");
						handle1.x = (handle1.x - handle5.x) / (handle1.y - handle5.y) * (handle1.y - handle2.y) + handle2.x;
						handle1.y = handle1.y;
						
					}
					else if (handle1.y != handle5.y && handle1.y != handle0.y)
					{
						trace("m");
						var k1:Number = (handle1.x - handle5.x) / (handle1.y - handle5.y);
						var k2:Number = (handle1.x - handle0.x) / (handle1.y - handle0.y);
						var temp:Number = handle1.y;
						handle1.y = (handle1.x -handle2.x + handle2.y * k1 - handle1.y * k2) / (k1 - k2);
						handle1.x = (handle1.x - handle5.x) / (temp - handle5.y) * (handle1.y - handle2.y) + handle2.x;
					}
					else
					{
						trace("[");
						handle1.x = handle0.x + handle2.x - handle3.x;
						handle1.y = handle0.y + handle2.y - handle3.y;
					}
				}
				//-------------------------------
				if (currentHandle == handle3)
				{
					trace(handle0.x, handle0.y, handle1.x, handle1.y, handle2.x, handle2.y, handle3.x, handle3.y);
					
					if (handle7.y == handle0.y && handle0.y != handle1.y)
					{	trace("a");					
						handle0.x = (handle0.x - handle1.x) / (handle0.y - handle1.y) * (handle3.y - handle0.y) + handle0.x;
						handle0.y = handle3.y;
					}
					else if (handle0.y == handle1.y && handle7.y != handle0.y)
					{
						trace("b");	
						if (handle0.x != handle7.x)
						{	
							handle0.x = (handle0.x - handle7.x) / (handle0.y - handle7.y) * (handle0.y - handle3.y) + handle3.x;
						}
						else
						{
							handle0.x = handle3.x;
						}
						
						handle0.y = handle0.y;
					}
					else if (handle0.y != handle1.y && handle7.y != handle0.y)
					{
						trace("c");	
						var k1:Number = (handle0.x - handle7.x) / (handle0.y - handle7.y);
						var k2:Number = (handle0.x - handle1.x) / (handle0.y - handle1.y);
						var temp:Number = handle0.y;
						handle0.y = (handle0.x -handle3.x + handle3.y * k1 - handle0.y * k2) / (k1 - k2);
						handle0.x = (handle0.x - handle7.x) / (temp - handle7.y) * (handle0.y - handle3.y) + handle3.x;
					}
					else 
					{
						trace("d");	
						trace(handle0.x);
						handle0.x = handle1.x - handle2.x + handle3.x;
						handle0.y = handle1.y;
					}
					
					if (handle6.y == handle2.y && handle2.y != handle1.y)
					{
						trace("e");	
						
						handle2.x = (handle2.x - handle1.x) / (handle2.y - handle1.y) * (handle3.y - handle2.y) + handle2.x;
						
						handle2.y = handle3.y;
					}
					else if (handle2.y == handle1.y && handle6.y != handle2.y)
					{
						trace("f");
						if (handle2.x != handle5.x)
						{
							handle2.x = (handle2.x - handle6.x) / (handle2.y - handle6.y) * (handle2.y - handle3.y) + handle3.x;
						}
						else
						{
							handle2.x = handle1.x;
						}
						handle2.y = handle2.y;
					}
					else if (handle1.y != handle2.y && handle2.y != handle6.y)
					{
						trace("g");	
						var k1:Number = (handle2.x - handle6.x) / (handle2.y - handle6.y);
						var k2:Number = (handle2.x - handle1.x) / (handle2.y - handle1.y);
						var temp:Number = handle2.y;
						handle2.y = (handle2.x -handle3.x + handle3.y * k1 - handle2.y * k2) / (k1 - k2);
						handle2.x = (handle2.x - handle6.x) / (temp - handle6.y) * (handle2.y - handle3.y) + handle3.x;
					}
					else
					{
						trace("[");
						handle2.x = handle1.x + handle3.x - handle0.x;
						handle2.y = handle3.y ;
					}
				}
				
				//-------------------------
				if (currentHandle == handle4)
				{
					if (handle0.y == handle1.y)
					{						
						//handle1.x = (handle1.x - handle2.x) / (handle1.y - handle2.y) * (handle4.y - handle1.y) + handle1.x;
						handle1.y = handle4.y;
						//handle0.x = 2 * handle4.x - handle1.x;
						handle0.y = 2 * handle4.y - handle1.y;
					}
					else if (handle1.y == handle2.y)
					{
						//handle1.x = (handle1.x - handle0.x) / (handle1.y - handle0.y) * (handle1.y - handle4.y) + handle4.x;
						handle1.y = handle1.y;
						//handle0.x = 2 * handle4.x - handle1.x;
						handle0.y = 2 * handle4.y - handle1.y;
					}
					else
					{
						var k1:Number = (handle1.x - handle0.x) / (handle1.y - handle0.y);
						var k2:Number = (handle1.x - handle2.x) / (handle1.y - handle2.y);
						var temp:Number = handle1.y;
						handle1.y = (handle1.x -handle4.x + handle4.y * k1 - handle1.y * k2) / (k1 - k2);
						//handle1.x = (handle1.x - handle0.x) / (temp - handle0.y) * (handle1.y - handle4.y) + handle4.x;
						//handle0.x = 2 * handle4.x - handle1.x;
						handle0.y = 2 * handle4.y - handle1.y;
					}
				}
				//---------------
				if (currentHandle == handle7)
				{
					trace(handle0.x, handle0.y, handle1.x, handle1.y, handle2.x, handle2.y, handle3.x, handle3.y);
					
					if (handle3.y == handle0.y)
					{   trace("q");						
						handle0.x = (handle0.x - handle1.x) / (handle0.y - handle1.y) * (handle7.y - handle0.y) + handle0.x;
						//	handle0.y = handle3.y;
						handle3.x = 2 * handle7.x - handle0.x;
						//	handle3.y = 2 * handle7.y - handle0.y;
					}
					else if (handle1.y == handle0.y)
					{  trace("w");	
						handle0.x = (handle0.x - handle3.x) / (handle0.y - handle3.y) * (handle0.y - handle7.y) + handle7.x;
						//	handle0.y = handle0.y;
						handle3.x = 2 * handle7.x - handle0.x;
						//	handle3.y = 2 * handle7.y - handle0.y;
					}
					else
					{  trace("e");	
						var k1:Number = (handle0.x - handle3.x) / (handle0.y - handle3.y);
						var k2:Number = (handle0.x - handle1.x) / (handle0.y - handle1.y);
						var temp:Number = handle0.y;
						//	handle0.y = (handle0.x -handle3.x + handle3.y * k1 - handle0.y * k2) / (k1 - k2);
						handle0.x = (handle0.x - handle7.x) / (temp - handle7.y) * (handle0.y - handle7.y) + handle7.x;
						handle3.x = 2 * handle7.x - handle0.x;
						//	handle3.y = 2 * handle7.y - handle0.y;
					}
				}//-------------
				if (currentHandle == handle6)
				{
					if (handle2.y == handle3.y)
					{						
						//	handle3.x = (handle3.x - handle0.x) / (handle3.y - handle0.y) * (handle6.y - handle3.y) + handle3.x;
						handle3.y = handle6.y;
						//	handle2.x = 2 * handle6.x - handle3.x;
						handle2.y = 2 * handle6.y - handle3.y;
					}
					else if (handle3.y == handle0.y)
					{
						//	handle3.x = (handle3.x - handle2.x) / (handle3.y - handle2.y) * (handle3.y - handle6.y) + handle6.x;
						handle3.y = handle3.y;
						//	handle2.x = 2 * handle6.x - handle3.x;
						handle2.y = 2 * handle6.y - handle3.y;
					}
					else
					{
						var k1:Number = (handle3.x - handle2.x) / (handle3.y - handle2.y);
						var k2:Number = (handle3.x - handle0.x) / (handle3.y - handle0.y);
						var temp:Number = handle3.y;
						handle3.y = (handle3.x -handle6.x + handle6.y * k1 - handle3.y * k2) / (k1 - k2);
						//	handle3.x = (handle3.x - handle2.x) / (temp - handle2.y) * (handle3.y - handle6.y) + handle6.x;
						//	handle2.x = 2 * handle6.x - handle3.x;
						handle2.y = 2 * handle6.y - handle3.y;
					}
				}
				//----------------------
				if (currentHandle == handle5)
				{
					if (handle1.y == handle2.y)
					{						
						handle2.x = (handle2.x - handle3.x) / (handle2.y - handle3.y) * (handle5.y - handle2.y) + handle2.x;
						//	handle2.y = handle5.y;
						handle1.x = 2 * handle5.x - handle2.x;
						//	handle1.y = 2 * handle5.y - handle2.y;
					}
					else if (handle2.y == handle3.y)
					{
						handle2.x = (handle2.x - handle1.x) / (handle2.y - handle1.y) * (handle2.y - handle5.y) + handle5.x;
						//	handle2.y = handle2.y;
						handle1.x = 2 * handle5.x - handle2.x;
						//	handle1.y = 2 * handle5.y - handle2.y;
					}
					else
					{
						var k1:Number = (handle2.x - handle1.x) / (handle2.y - handle1.y);
						var k2:Number = (handle2.x - handle3.x) / (handle2.y - handle3.y);
						var temp:Number = handle2.y;
						//	handle2.y = (handle2.x -handle5.x + handle5.y * k1 - handle2.y * k2) / (k1 - k2);
						handle2.x = (handle2.x - handle1.x) / (temp - handle1.y) * (handle2.y - handle5.y) + handle5.x;
						handle1.x = 2 * handle5.x - handle2.x;
						//	handle1.y = 2 * handle5.y - handle2.y;
					}
				}			
			}
			
			handle4.x = (handle0.x + handle1.x) / 2;
			handle4.y = (handle0.y + handle1.y) / 2;
			handle5.x = (handle2.x + handle1.x) / 2;
			handle5.y = (handle2.y + handle1.y) / 2;
			handle6.x = (handle2.x + handle3.x) / 2;
			handle6.y = (handle2.y + handle3.y) / 2;
			handle7.x = (handle0.x + handle3.x) / 2;
			handle7.y = (handle0.y + handle3.y) / 2;
			if (handle0 != handle1 || handle1 != handle2 || handle2 != handle3 || handle3!= handle4)
			{
				vertices[0] = handle0.x;
				vertices[1] = handle0.y;
				vertices[2] = handle1.x;
				vertices[3] = handle1.y;
				vertices[4] = handle2.x;
				vertices[5] = handle2.y;
				vertices[6] = handle3.x;
				vertices[7] = handle3.y;
				
			}			

			this.graphics.clear();
			this.graphics.beginBitmapFill(bitmap.bitmapData);
			this.graphics.drawTriangles(vertices, indices, uvtData);
			this.graphics.endFill();
			
			sprite.graphics.clear();
			sprite.graphics.beginBitmapFill(bitmap.bitmapData);
			sprite.graphics.drawTriangles(vertices, indices, uvtData);
			sprite.graphics.endFill();

			if (isShow)
			{
				this.graphics.lineStyle(1, 0xFF0000);
				this.graphics.moveTo(vertices[0], vertices[1]);
				this.graphics.lineTo(vertices[2], vertices[3]);
				this.graphics.lineTo(vertices[4], vertices[5]);
				this.graphics.lineTo(vertices[6], vertices[7]);
				this.graphics.lineTo(vertices[0], vertices[1]);	
			}	
			
			sprite.filters = filterArray;
		}
		
		public function setFilter(filter:DropShadowFilter):void
		{
			filterArray = [];
			this.filter = filter;
			filterArray.push(filter);
			sprite.filters = filterArray;
			
		}
		
		private function rTfc():void
		{
			tfc.x = this.mouseX;
			tfc.y = this.mouseY;
		}
		public function isShowDot(isShow:Boolean):void
		{
			handle0.visible = isShow;
			handle1.visible = isShow;
			handle2.visible = isShow;
			handle3.visible = isShow;
			
			handle4.visible = isShow;
			handle5.visible = isShow;
			handle6.visible = isShow;
			handle7.visible = isShow;
			
			if (isShow)
			{
				this.graphics.lineStyle(1, 0xFF0000);
				this.graphics.moveTo(vertices[0], vertices[1]);
				this.graphics.lineTo(vertices[2], vertices[3]);
				this.graphics.lineTo(vertices[4], vertices[5]);
				this.graphics.lineTo(vertices[6], vertices[7]);
				this.graphics.lineTo(vertices[0], vertices[1]);	
			}	
			else
				this.graphics.clear();
		}
	}
}